package com.zy.admin.controller;

import com.zy.admin.entity.Admin;
import com.zy.admin.entity.TeacherFunctionShiro;
import com.zy.admin.service.IAdminService;
import com.zy.admin.service.ITeacherFunctionShiroService;
import com.zy.studentSide.service.IStudentService;
import com.zy.teacherSide.entity.TeacherShiro;
import com.zy.teacherSide.service.ITeacherService;
import com.zy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 29787
 */
@RestController
public class AdminController {

    @Autowired
    private IAdminService iAdminService;

    @Autowired
    private ITeacherService iTeacherService;

    @Autowired
    private IStudentService iStudentService;

    @Autowired
    private ITeacherFunctionShiroService iTeacherFunctionShiroService;

    /**
     * 管理员登录
     *
     * @param admin
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/adminLogin")
    public R adminLogin(@RequestBody Admin admin) {
        Admin a = iAdminService.getAdmin(admin.getAdminNum(), admin.getAdminPassword());
        return new R(a == null ? false : true, a);
    }

    /**
     * 管理员获取所有教师信息
     *
     * @return R
     */
    @GetMapping
    @RequestMapping(value = "/adminGetAllTeacher")
    public R adminGetAllTeacher() {
        return new R(true, iTeacherService.list());
    }

    /**
     * 保存教师权限数据
     *
     * @param teacherShiro
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/adminToTeacherGrantAuthorization")
    public R adminToTeacherGrantAuthorization(@RequestBody TeacherShiro teacherShiro) {
        Integer[] arr = teacherShiro.getArr();
        TeacherFunctionShiro tfs = new TeacherFunctionShiro();
        tfs.setTeacherId(teacherShiro.getTeacherId());
        Boolean b = true;
        if (arr[1] == 2) {
            tfs.setAddStudentShiro(1);
        } else {
            b = false;
            tfs.setAddStudentShiro(0);
        }
        if (arr[2] == 3) {
            tfs.setStudentListShiro(1);
        } else {
            b = false;
            tfs.setStudentListShiro(0);
        }
        if (arr[4] == 5) {
            tfs.setAddClassroomShiro(1);
        } else {
            b = false;
            tfs.setAddClassroomShiro(0);
        }
        if (arr[5] == 6) {
            tfs.setClassroomListShiro(1);
        } else {
            b = false;
            tfs.setClassroomListShiro(0);
        }
        if (arr[7] == 8) {
            tfs.setAddCourseShiro(1);
        } else {
            b = false;
            tfs.setAddCourseShiro(0);
        }
        if (arr[8] == 9) {
            tfs.setCourseListShiro(1);
        } else {
            b = false;
            tfs.setCourseListShiro(0);
        }
        if (arr[10] == 11) {
            tfs.setAddTaskShiro(1);
        } else {
            b = false;
            tfs.setAddTaskShiro(0);
        }
        if (arr[11] == 12) {
            tfs.setTaskListShiro(1);
        } else {
            b = false;
            tfs.setTaskListShiro(0);
        }
        if (arr[14] == 15) {
            tfs.setStudentAgeEchartsShiro(1);
        } else {
            b = false;
            tfs.setStudentAgeEchartsShiro(0);
        }
        if (arr[16] == 17) {
            tfs.setCourseCreditEchirtsShiro(1);
        } else {
            b = false;
            tfs.setCourseCreditEchirtsShiro(0);
        }
        if (arr[18] == 19) {
            tfs.setTaskSubmitEchartsShiro(1);
        } else {
            b = false;
            tfs.setTaskSubmitEchartsShiro(0);
        }
        if (arr[19] == 20) {
            tfs.setStudentAchievementsEchartsShiro(1);
        } else {
            b = false;
            tfs.setStudentAchievementsEchartsShiro(0);
        }
        boolean s = iTeacherFunctionShiroService.save(tfs);
        Boolean boo = false;
        if (b){
            // 更新教师权限
            boo = iTeacherService.updateTeacher(1, teacherShiro.getTeacherId());
        } else {
            boo = iTeacherService.updateTeacher(10, teacherShiro.getTeacherId());
        }
        return new R(s && boo);
    }

    /**
     * 管理员获取所有学生信息
     * @return R
     */
    @GetMapping
    @RequestMapping(value = "/adminGetAllStudent")
    public R getAllStudent(){
        return new R(true,iStudentService.list());
    }
}
